﻿using ChangingValidationFromAndbyAggregate.Dominio;
using ChangingValidationFromAndbyAggregate.Validators;
using NHibernate.Validator.Cfg.Loquacious;
using NHibernate.Validator.Engine;
using NUnit.Framework;
using SharpTestsEx;

namespace ChangingValidationFromAndbyAggregate.Test.Dominio
{
    /// <summary>
    /// Implementation by Fabio Maulo (http://http://fabiomaulo.blogspot.com.br/search/label/Validator
    /// </summary>
    [TestFixture]
    public class BusinessRuleDemo
    {
        ValidatorEngine _validatorEngine;

        [TestFixtureSetUp]
        public void CreateValidatorEngine()
        {
            var configure = new FluentConfiguration();

            var implementacoesDeValidacaoASeremConsideradas = new[]
                                                                  {
                                                                      typeof (CustomerValidation),
                                                                      typeof (InvoiceValidation)
                                                                  };

            configure
                .Register(implementacoesDeValidacaoASeremConsideradas)
                .SetDefaultValidatorMode(ValidatorMode.UseExternal);

            _validatorEngine = new ValidatorEngine();

            _validatorEngine.Configure(configure);
        }

        [Test]
        public void CustomerValidWhenHaveCompanyName()
        {
            var customer = new Customer
                               {
                                   CompanyName = "ACME"
                               };
            customer.Satisfy(c => _validatorEngine.IsValid(c));
        }

        [Test]
        public void CustomerNotValidWhenHaveInvalidVatIdentificationNumber()
        {
            var customer = new Customer
                               {
                                   CompanyName = "ACME", VatIdentificationNumber = "123"
                               };
            customer.Satisfy(c => !_validatorEngine.IsValid(c));
        }

        [Test]
        public void CustomerValidWhenHaveValidVatIdentificationNumber()
        {
            var customer = new Customer
                               {
                                   CompanyName = "ACME", VatIdentificationNumber = "01636120634"
                               };
            customer.Satisfy(c => _validatorEngine.IsValid(c));
        }

        [Test]
        public void InvoiceNotValidWhenDoesNotHaveCustomer()
        {
            var invoice1 = new Invoice();
            invoice1.Satisfy(invoice => !_validatorEngine.IsValid(invoice));
        }

        [Test]
        public void InvoiceNotValidWhenHaveCustomerWithoutVatIdentificationNumber()
        {
            (new Invoice { Customer = new Customer { CompanyName = "ACME" } })
                .Satisfy(invoice => !_validatorEngine.IsValid(invoice));
        }

        [Test]
        public void InvoiceNotValidWhenHaveCustomerWithInvalidVatIdentificationNumber()
        {
            var invoice1 = new Invoice
                               {
                                   Customer = new Customer {CompanyName = "ACME", VatIdentificationNumber = "123"}
                               };
            invoice1.Satisfy(invoice => !_validatorEngine.IsValid(invoice));
        }

        [Test]
        public void InvoiceValidWhenHaveCustomerWithValidVatIdentificationNumber()
        {
            var invoice1 = new Invoice
                               {
                                   Customer = new Customer
                                                  {
                                                      CompanyName = "ACME", VatIdentificationNumber = "01636120634"
                                                  }
                               };
            invoice1.Satisfy(invoice => _validatorEngine.IsValid(invoice));
        }
    }
}
